<!--管理员登陆页面-->
<template >
  <div>
    <div style="width: 600px; background: #fff; margin: 50px auto; padding: 30px 60px;box-shadow: 0px 2px 12px 0px rgba(0,0,0,0.1)">
        <img src="http://114.55.145.59/rctms/img/logo-logo.png"style="margin:30px 15px">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-row>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
        </el-row>
       <el-row>
         <el-form-item label="密码" prop="password">
           <el-input v-model="ruleForm.password"></el-input>
         </el-form-item>
       </el-row>
      <el-row>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-row>
      </el-form>
    </div>

  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9081/admins/login';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm);
          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '登录成功！',
                type: 'success'
              });
              // 获取服务器端响应的JWT，并存储到LocalStorage
              let jwt = responseBody.data;
              console.log('登录成功，服务器响应的JWT=' + jwt);
              localStorage.setItem('jwt', jwt);
              // 以下是测试
              // let localJwt = localStorage.getItem('jwt');
              // console.log('从LocalStorage中读取JWT：' + localJwt);
              location.href="http://localhost:9090/test"
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>
body{
  font-size: 14px;
  line-height: 1.42857143;
  background-color: #fff;
}
</style>
